﻿using Payroll.BusinessLogic.Devices;
using Payroll.Common.Utils;
using PayrollHR.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PayrollHR.Administration.Modules
{
    public partial class AddDeviceControl : BaseAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Bind();
            }
        }
        public int DeviceID
        {
            get
            {
                return CommonHelper.QueryStringInt("DeviceID");
            }
        }
        protected void Bind()
        {
            if (this.DeviceID > 0)
            {
                Devices deviceColletion = this.DeviceService.GetByDeviceID(this.DeviceID);
                if (!CommonHelper.IsNull(deviceColletion))
                {
                    txtDeviceCode.Text = deviceColletion.DeviceCode;
                    txtDeviceName.Text = deviceColletion.DeviceName;
                    txtNote.Text = deviceColletion.Note;
                }
            }
        }
        protected Devices GetDevice()
        {
            return new Devices
            {
                DeviceID = this.DeviceID,
                DeviceCode = txtDeviceCode.Text.Trim(),
                DeviceName = txtDeviceName.Text.Trim(),
                Note = txtNote.Text.Trim(),
                CreateDate = DateTime.Now,
                ModifiedDate = DateTime.Now
            };
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {            
            {
                try
                {
                    int deviceId = this.DeviceService.Save(this.GetDevice());
                    if (Page.IsValid)
                        Response.Redirect("DeviceList.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }

        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    int deviceId = this.DeviceService.Save(this.GetDevice());
                    Response.Redirect(String.Format("AddDevice.aspx?DeviceID={0}", deviceId));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
    }
}